package hos.java.compat;


/**
 * <p>Title: FileCompat </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2021/7/12 13:55
 */
public class FileTypeCompat {


    /**
     * 根据文件的路径获取文件的类型String
     *
     * @param filePath
     * @return
     */
    public static String getFileTypeString(String filePath) {
        if (isFileType(filePath, Type.AUDIO)) {
            return Type.AUDIO.getType();
        } else if (isFileType(filePath, Type.IMAGE)) {
            return Type.IMAGE.getType();
        } else if (isFileType(filePath, Type.PDF)) {
            return Type.PDF.getType();
        } else if (isFileType(filePath, Type.TXT)) {
            return Type.TXT.getType();
        } else if (isFileType(filePath, Type.AUDIO)) {
            return Type.AUDIO.getType();
        } else if (isFileType(filePath, Type.VIDEO)) {
            return Type.VIDEO.getType();
        } else if (isFileType(filePath, Type.CHM)) {
            return Type.CHM.getType();
        } else if (isFileType(filePath, Type.WORD)) {
            return Type.WORD.getType();
        } else if (isFileType(filePath, Type.EXCEL)) {
            return Type.EXCEL.getType();
        } else if (isFileType(filePath, Type.PPT)) {
            return Type.PPT.getType();
        } else if (isFileType(filePath, Type.APK)) {
            return Type.APK.getType();
        }
        return "*/*";
    }

    /**
     * 判断是否为指定类型的文件
     *
     * @param fileName
     * @param type
     * @return
     */
    public static boolean isFileType(String fileName, Type type) {
        /* 依扩展名的类型决定MimeType */
        String fileEnd = "";
        try {
            int lastIndexOf = fileName.lastIndexOf(".");
            fileEnd = fileName.substring(lastIndexOf, fileName.length()).toLowerCase();
        } catch (Exception e) {
        }
        if (Type.AUDIO.equals(type)
                && (equalsIgnoreCase(fileEnd, ".m4a") || equalsIgnoreCase(fileEnd, ".mp3")
                || equalsIgnoreCase(fileEnd, ".mid") || equalsIgnoreCase(fileEnd, ".wav")
                || equalsIgnoreCase(fileEnd, ".xmf") || equalsIgnoreCase(fileEnd, ".ogg") || equalsIgnoreCase(fileEnd, "" + ".amr"))) {
            return true;
        } else if (Type.VIDEO.equals(type)
                && (equalsIgnoreCase(fileEnd, ".3gp") || equalsIgnoreCase(fileEnd, ".mp4")
                || equalsIgnoreCase(fileEnd, "" + "" + ".mts"))) {
            return true;
        } else if (Type.IMAGE.equals(type)
                && (equalsIgnoreCase(fileEnd, ".jpg") || equalsIgnoreCase(fileEnd, ".gif")
                || equalsIgnoreCase(fileEnd, ".png") || equalsIgnoreCase(fileEnd, ".jpeg")
                || equalsIgnoreCase(fileEnd, ".bmp"))) {
            return true;
        } else if (Type.EXCEL.equals(type)
                && (equalsIgnoreCase(fileEnd, ".xls") || equalsIgnoreCase(fileEnd, ".xlsx"))) {
            return true;
        } else if (Type.TXT.equals(type) && equalsIgnoreCase(fileEnd, ".txt")) {
            return true;
        } else if (Type.WORD.equals(type)
                && (equalsIgnoreCase(fileEnd, ".doc") || equalsIgnoreCase(fileEnd, ".chm")
                || equalsIgnoreCase(fileEnd, ".docx"))) {
            return true;
        } else if (Type.PDF.equals(type) && (equalsIgnoreCase(fileEnd, ".pdf"))) {
            return true;
        } else if (Type.PPT.equals(type)
                && (equalsIgnoreCase(fileEnd, ".ppt") || equalsIgnoreCase(fileEnd, ".dps"))) {
            return true;
        } else if (Type.APK.equals(type) && (equalsIgnoreCase(fileEnd, ".apk"))) {
            return true;
        } else if (Type.HTML.equals(type) && (equalsIgnoreCase(fileEnd, ".html"))) {
            return true;
        }
        return false;
    }

    /**
     * 判断两字符串忽略大小写是否相等
     *
     * @param a 待校验字符串a
     * @param b 待校验字符串b
     * @return {@code true}: 相等<br>{@code false}: 不相等
     */
    private static boolean equalsIgnoreCase(final String a, final String b) {
        return a == null ? b == null : a.equalsIgnoreCase(b);
    }

    public static String getFilePathString(String fileName) {
        if (isFileType(fileName, Type.AUDIO)) {
            return "audio";
        } else if (isFileType(fileName, Type.IMAGE)) {
            return "image";
        } else if (isFileType(fileName, Type.PDF)) {
            return "pdf";
        } else if (isFileType(fileName, Type.TXT)) {
            return "txt";
        } else if (isFileType(fileName, Type.VIDEO)) {
            return "video";
        } else if (isFileType(fileName, Type.CHM)) {
            return "chm";
        } else if (isFileType(fileName, Type.WORD)) {
            return "word";
        } else if (isFileType(fileName, Type.EXCEL)) {
            return "excel";
        } else if (isFileType(fileName, Type.PPT)) {
            return "ppt";
        } else if (isFileType(fileName, Type.APK)) {
            return "apk";
        }
        return "common";
    }

    public enum Type {

        HTML("text/html"), IMAGE("image/*"), PDF("application/pdf"), TXT("text/plain"), AUDIO("audio/*"), VIDEO("video/*"), CHM(
                "application/x-chm"), WORD("application/msword"), EXCEL("application/vnd.ms-excel"), PPT("application/vnd.ms-powerpoint"), APK(
                "application/vnd.android.package-archive");

        private String type;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        Type(String type) {
            this.type = type;
        }
    }

}
